package com.coldworks.coldjoke.activity;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.coldworks.base.BaseActivity;
import com.coldworks.base.manager.BaseBitmapManager;
import com.coldworks.base.manager.BaseDisplayManager;
import com.coldworks.base.manager.BaseStorageManager;
import com.coldworks.base.ui.zoom.ImageViewTouch;
import com.coldworks.coldjoke.R;

public class ViewImageActivity extends BaseActivity {

	private ImageViewTouch mImage;
	private String imgName;
	private Bitmap bitmap;
	private RelativeLayout operationBar;
	private Button backBtn;
	private Button saveBtn;
	private PopupWindow vPopupWindow;
	private String path;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.activity_view_img);
		setUpViews();
		setUpListeners();
		// 压缩，避免爆了内存
		Bundle b = getIntent().getExtras();
		if (b != null) {
			if (b.containsKey("imgPath")) {
				BaseDisplayManager dm = BaseDisplayManager.getInstance();
				path = b.getString("imgPath");
				// path = BaseStorageManager.getInstance().getImgDir()
				// + File.separator + imgName;
				bitmap = BitmapFactory.decodeFile(path);
				mImage.setImageBitmap(bitmap);
			}
		} else {
			finish();
		}

	}

	private void setUpViews() {
		setUpPopupWindow();
		mImage = (ImageViewTouch) findViewById(R.id.view_img);
		operationBar = (RelativeLayout) findViewById(R.id.operation_bar_image);
		backBtn = (Button) findViewById(R.id.button_back);
		saveBtn = (Button) findViewById(R.id.button_save);
	}

	private void setUpListeners() {
		mImage.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (!vPopupWindow.isShowing()) {
					log.info("fuck test");
				}
			}
		});

		backBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mImage.setImageBitmap(null);
				if (bitmap != null) {
					bitmap.recycle();
				}
				finish();
			}
		});
		saveBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				try {
					InputStream inputStream = new FileInputStream(path);
					String fileName = BaseBitmapManager.getInstance()
							.getFileNameByUrl(path);
					OutputStream outputStream = new FileOutputStream(
							BaseStorageManager.getInstance().getSaveDir()
									+ File.separator + fileName);
					byte bt[] = new byte[1024];
					int c;
					while ((c = inputStream.read(bt)) > 0) {
						outputStream.write(bt, 0, c);
					}
					inputStream.close();
					outputStream.close();
					Toast.makeText(context, R.string.view_img_save_succ,
							Toast.LENGTH_LONG).show();
				} catch (Exception e) {
					log.info("", e);
					Toast.makeText(context, R.string.view_img_save_err,
							Toast.LENGTH_LONG).show();
				}

			}
		});
	}

	private void setUpPopupWindow() {
		operationBar = (RelativeLayout) LayoutInflater.from(context).inflate(
				R.layout.operation_bar_image, null);
		vPopupWindow = new PopupWindow(operationBar, LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);
		vPopupWindow.update();
		vPopupWindow.setFocusable(true);
		vPopupWindow.setAnimationStyle(android.R.style.Animation_Toast);
		vPopupWindow.setBackgroundDrawable(new BitmapDrawable());
	}

}
